package com.my.strings;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author mars
 * @date 2024-09-14 09:16
 */
public class StringJoin {
    public static void main(String[] args) {

        List<String> list = Arrays.asList("张三", "李四", "王武", null, "麻子");
        //java stream流中的Collections.joining方法
        //指定分隔符
        String collect = list.stream().collect(Collectors.joining(","));
        //没有分隔符，直接拼接
        String collect1 = list.stream().collect(Collectors.joining());
        //指定分隔符，前缀，后缀
        String collect2 = list.stream().collect(Collectors.joining(",", "{", "}"));
        String collect3 = list.stream().collect(Collectors.joining(",", "[", "]"));
        System.out.println(collect);//张三,李四,王武,null,麻子
        System.out.println(collect1);//张三李四王武null麻子
        System.out.println(collect2);//{张三,李四,王武,null,麻子}
        System.out.println(collect3);//[张三,李四,王武,null,麻子]

        List<Integer> list1 = Arrays.asList(1, 2, 3, 4, 5);
        String collect4 = list1.stream().map(Object::toString).collect(Collectors.joining(",", "[", "]"));
    }
}
